package com.streetsmart.test;
/**
 * used to retrieve geo point for an address and address for geo points 
 */
import org.json.JSONException;
import com.streetsmart.R;
import com.streetsmart.MapRouteActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.streetsmart.Utilities;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;

public class GeocodeActivity extends Activity {
	EditText lat;
	EditText lng;
	EditText address;
	Button button;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		lat = (EditText) findViewById(R.id.latitude);
		lng = (EditText) findViewById(R.id.longitude);
		address = (EditText) findViewById(R.id.address);
		setContentView(R.layout.geocode);
		Utilities.clearGeopoints();
	}
	
	@Override
	public void onBackPressed(){
		finish();
	}
	
	public void getAddress(View v) throws JSONException{
		lat = (EditText) findViewById(R.id.latitude);
		lng = (EditText) findViewById(R.id.longitude);
		address = (EditText) findViewById(R.id.address);
		double[] darray = {Double.parseDouble(lat.getText().toString()),Double.parseDouble(lng.getText().toString())};
		String addr = Utilities.getAddress(darray);
		address.setText(addr);
	}
	
	public void getCoordinates(View v) throws JSONException {
		lat = (EditText) findViewById(R.id.latitude);
		lng = (EditText) findViewById(R.id.longitude);
		address = (EditText) findViewById(R.id.address);
		double[] coordinate = Utilities.getLatAndLng(address.getText().toString());
		lat.setText(coordinate[0] + "");
		lng.setText(coordinate[1] + "");
	}
	
	public void addToMap(View v){
		lat = (EditText) findViewById(R.id.latitude);
		lng = (EditText) findViewById(R.id.longitude);
		address = (EditText) findViewById(R.id.address);
		button = (Button) findViewById(R.id.mapview);
		Double latitude = Double.parseDouble(lat.getText().toString());
		Double longitude = Double.parseDouble(lng.getText().toString());
		Utilities.latitudeList.add(latitude);
		Utilities.longitudeList.add(longitude);
		//TODO hadle cases like enable viewMap button only after one entry atleast is added.
	}
	
	public void viewMap(View v){
		Intent intent = new Intent(this, MapRouteActivity.class);
		intent.putExtra("switched", true);
		startActivity(intent);
	}
}